home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #44 (May 89) / Cursor Stuff / main.c < prev    next >
Text File  |  1989-02-17  |  4KB  |  93 lines

  1. /*    Animated Cursor demonstration main program.
  2.  
  3.         Copyright © 1989 by Michael S. Morton, all rights reserved.
  4.         Written February ’89 for MacTutor.
  5. */
  6.  
  7. #include "AnimCurs.h"                                        /* define functions in package */
  8.  
  9. /*    Internal prototypes: */
  10. void main (void);
  11. void makeMenus (void);
  12. void doMenu (long result);
  13.  
  14. int theDelay = 4;                                                /* delay between animation frames, tix */
  15.  
  16. enum                                                                        /* items in our sole menu */
  17. {    mnWatch = 1, mnWatch2, mnHourG, mnUnused1,
  18.     mnFaster, mnSlower, mnTop, mnUnused2, mnQuit };
  19.  
  20. void main ()
  21. {    EventRecord evtRec;                                        /* info from GetNextEvent () */
  22.     int inWhat;                                                        /* from FindWindow (): where was click? */
  23.     WindowPtr    theWin;                                            /* unused -- just for FindWindow () */
  24.     long menuResult;                                            /* from MenuKey () or MenuSelect () */
  25.  
  26.     /*    The usual initializations: perhaps more than we need here. */
  27.     InitGraf (&thePort);                                    /* initialize QuickDraw */
  28.     InitFonts ();                                                    /* init the Font Manager */
  29.     FlushEvents (everyEvent, 0);                    /* ignore pending events */
  30.     InitWindows ();                                                /* init the Window Manager */
  31.     InitMenus ();                                                    /* init the Menu Manager */
  32.     TEInit ();                                                        /* init TextEdit */
  33.     InitDialogs (0L);                                            /* init Dialog Mgr; no restart proc */
  34.     InitCursor ();                                                /* show standard arrow cursor */
  35.  
  36.     makeMenus ();                                                    /* set up one cheap little menu */
  37.     acStartW1 (theDelay);                                    /* initialize to a one-handed watch */
  38.  
  39.     /*    Main loop: at idle time, update the cursor.  Check for menu commands
  40.             via the mouse or keyboard. */
  41.     while (1)
  42.     {    acNext ();                                                    /* idle time: animate the cursor */
  43.         SystemTask ();                                            /* can’t hurt, altho we don’t do DAs */
  44.  
  45.         if (GetNextEvent (everyEvent, & evtRec))    /* try for an event */
  46.         {    if (evtRec.what == mouseDown)            /* click? */
  47.             {    inWhat = FindWindow (evtRec.where, &theWin); /* look up where it hit */
  48.                 if (inWhat == inMenuBar)                /* menu click? */
  49.                     doMenu (MenuSelect(& (evtRec.where))); /* track & process menu hit */
  50.                 else SysBeep (2);                                /* can’t handle clicks elsewhere */
  51.             }                                                                    /* end of handling mouseDown */
  52.  
  53.             else if (evtRec.what = keyDown)        /* keypress?  */
  54.                 doMenu (MenuKey((char) evtRec.message)); /* don’t require cmdKey */
  55.         }                                                                        /* end of handling one event */
  56.  
  57.     }                                                                            /* end of infinite loop */
  58. }                                                                                /* end of main () */
  59.  
  60. /*    Make menu in-line.  (Resources?  This is just a demo driver…) */
  61. void makeMenus ()
  62. {    MenuHandle theMenu;                                        /* our sole menu */
  63.  
  64.     theMenu = NewMenu (1, "\pMenu");
  65.     AppendMenu (theMenu, "\pWatch/W;Two-handed watch/2;Hourglass/H;(-");
  66.     AppendMenu (theMenu, "\pFaster/F;Slower/S;Top speed/T;(-");
  67.     AppendMenu (theMenu, "\pQuit/Q;(-");
  68.     AppendMenu (theMenu, "\pAnimated Cursor Demo;written in LightspeedC");
  69.     AppendMenu (theMenu, "\pCopyright © 1989;Michael S. Morton;for MacTutor");
  70.     InsertMenu (theMenu, 0);                            /* put it in the menu bar */
  71.     DrawMenuBar ();                                                /* show our handiwork */
  72. }                                                                                /* end of makeMenus () */
  73.  
  74. void doMenu (result)
  75.     long result;                                                    /* INPUT: result from MenuSelect/Key */
  76. {    int command = LoWord (result);                /* extract item number */
  77.  
  78.     switch (command)
  79.     {    case mnWatch:        acStartW1 (theDelay); break;
  80.         case mnWatch2:    acStartW2 (theDelay); break;
  81.         case mnHourG:        acStartHG (theDelay); break;
  82.  
  83.         /*    Note that we can’t let the delay go down to zero for “Faster”. */
  84.         case mnFaster:    if (theDelay>1) --theDelay; acDelay (theDelay); break;
  85.         case mnSlower:    ++theDelay;                                 acDelay (theDelay); break;
  86.         case mnTop:            theDelay = 1;                             acDelay (theDelay); break;
  87.  
  88.         case mnQuit:        ExitToShell (); break;
  89.         default:                break;                            /* ignore unrecognized items */
  90.     }                                                                            /* end of switch on command */
  91.  
  92.     HiliteMenu (0);
  93. }                                                                                /* end of doMenu () */